<%@page import="edu.gatech.team404.Course"%>
<%@page import="edu.gatech.team404.dal.CourseDAL"%>
<%@page import="edu.gatech.team404.Account"%>
<%@page import="edu.gatech.team404.dal.AccountDAL"%>
<%@page import="edu.gatech.team404.tool.StatusCode"%>
<%@page import="java.util.ArrayList"%>

<%@include file="header.jsp" %>

<div id="content">
    <%@include file="sidebar.jsp" %>
    
    <%@include file="toolbar.jsp" %>
    
    <div id="main">
    	<h1 align="center">Course</h1>
    	
    	<div id="create">
	    	<div class="status">
		    	<%
		    	Object status = session.getAttribute("courseCreationStatus");
		    		if (status != null) {
		    			StatusCode code = (StatusCode) status;
		    			switch (code) {
		    			case COURSE_CREATE_SUCCESS :
		    				out.println("<p style=\"color:blue;\">");
							out.println("Course has been successfully created.");
							out.println("</p>");
		    				break;
		    			case COURSE_CREATE_FAIL :
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot create a Course. Please try again later.");
							out.println("</p>");
		    				break;
		    			case COURSE_CREATE_INVALID_DATA :
		    				out.println("<p style=\"color:red;\">");
							out.println("Invalid input(s).");
							out.println("</p>");
							break;
		    			}
		    		}
		    	%>
	    	</div>
	    	<div class="viewTable" ">
		    	<form action="CourseCD" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table cellpadding="10">
		    			<tr>
		    				<th align="right">Course Name</th>
		    				<td align="left"><input type="text" name="name" value="" size="50" maxlength="50"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Description</th>
		    				<td align="left"><input type="text" name="description" value="" size="50" maxlength="500"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Instructor</th>
		    				<td align="left">
		    					<select name="instructorName"> 
			    				<%
			    					ArrayList<Account> instruct = (ArrayList<Account>) acctDal.getAllInstructors();
			    					acctDal.close();
			    					for(Account acct : instruct){
			    						if (acct != null) {
			    		    				out.println ("<option value=\"" + acct.getUsername() + "\">" + acct.getName() + "</option>");
			    						}
			    					}
			    				%>
		    					</select>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Create Course!" />
		    				</td>
		    			</tr>
		    		</table>
		    	</form>
	    	</div>
    	</div>
    	
    	<div id="review">
	    	<table class="viewTable" border="1" cellpadding="10">
	    		<tr>
	    			<th>Name</th>
	    			<th>Description</th>
	    			<th>Instructor</th>
	    		</tr>
	
		    	<% 
		    	CourseDAL cDal = new CourseDAL();
		    	ArrayList<Course> corses = (ArrayList<Course>) cDal.getAllCourses();
		    	cDal.close();
	    		
	    		for (Course corse : corses) {
	    			if (corse != null) {
	    				out.println ("<tr>");
	    				out.println ("<td>" + corse.getName() + "</td>");
	    				out.println ("<td>" + corse.getDescription() + "</td>");
	    				
	    				String instructor = acctDal.getAccount(corse.getInstructor()).getName();
	    				
	    				out.println ("<td>" + instructor + "</td>");
	    				out.println ("</tr>");
	    			}
	    		}
	    		%>
	    	
	    	</table>
    	</div>
    	
    	<div id="update">
    		<div class="status">
		    	<%
		    	status = session.getAttribute("courseUpdateStatus");
		    		if (status != null) {
		    			StatusCode code = (StatusCode) status;
		    			switch (code) {
		    			case COURSE_UPDATE_SUCCESS :
		    				out.println("<p style=\"color:blue;\">");
							out.println("Course has been successfully updated.");
							out.println("</p>");
		    				break;
		    			case COURSE_UPDATE_FAIL :
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot update the Course. Please try again later.");
							out.println("</p>");
		    				break;
		    			case COURSE_UPDATE_INVALID_DATA :
		    				out.println("<p style=\"color:red;\">");
							out.println("Invalid input(s).");
							out.println("</p>");
							break;
		    			}
		    		}
		    	%>
	    	</div>
	    	<div>
		    	<form action="UpdateCourse" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table class="viewTable" cellpadding="10">
		    			<tr>
		    				<th align="right">Course Name</th>
		    				<td>
			    				<select name="name"> 
				    				<%
				    					for(Course corse : corses){
				    						if (corse != null) {
				    		    				out.println ("<option value=\"" + corse.getName() + "\">" + corse.getName() + "</option>");
				    						}
				    					}
				    				%>
			    				</select>
			    			</td>
		    			</tr>
		    			<tr>
		    				<th align="right">Description</th>
		    				<td align="left"><input type="text" name="description" value="" size="50" maxlength="500"/></td>
		    			</tr>
		    			<tr>
		    				<th align="right">Instructor</th>
		    				<td align="left">
		    					<select name="instructorName"> 
			    				<%
			    					for(Account acct : instruct){
			    						if (acct != null) {
			    		    				out.println ("<option value=\"" + acct.getUsername() + "\">" + acct.getName() + "</option>");
			    						}
			    					}
			    				%>
		    					</select>
		    				</td>
		    			</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Update Course!" />
		    				</td>
		    			</tr>
		    		</table>
		    	</form>
	    	</div>
    	</div>
    	
    	<div id="delete">
    		<div class="status">
		    	<%
		    	status = session.getAttribute("courseDeleteStatus");
		    		if (status != null) {
		    			StatusCode code = (StatusCode) status;
		    			switch (code) {
		    			case COURSE_DELETE_SUCCESS :
		    				out.println("<p style=\"color:blue;\">");
							out.println("Course has been successfully deleted.");
							out.println("</p>");
		    				break;
		    			case COURSE_DELETE_FAIL :
		    				out.println("<p style=\"color:red;\">");
							out.println("Cannot delete the Course. Please try again later.");
							out.println("</p>");
		    				break;
		    			}
		    		}
		    	%>
	    	</div>
	    	<div>
		    	<form action="DeleteCourse" method="post">
		    		<input type= "hidden" name="courseName" value='<%=request.getParameter("course") %>'>
		    		<table class="viewTable" cellpadding="10">
		    			<tr>
		    				<th align="right">Course Name</th>
		    				<td>
		    					<select name="name" onChange="Script404.courseDesc(this.value, 'courseDescription')">
		    						<option value="">Select course</option>
				    				<%
				    					for(Course corse : corses){
				    						if (corse != null) {
				    		    				out.println ("<option value=\"" + corse.getName() + "\">" + corse.getName() + "</option>");
				    						}
				    					}
				    				%>
			    				</select>
			    			</td>
		    			</tr>
		    			<tr>
							<td colspan=2 align""=left">
								<p id="courseDescription"></p>
							</td>
						</tr>
		    			<tr>
		    				<td colspan="2" align="center">
		    					<input type="submit" value="Delete Course!" />
		    				</td>
		    			</tr>
		    		</table>
		    	</form>
	    	</div>
    	</div>
    	
    	<script type="text/javascript"> 
			Script404.initShow("<%= session.getAttribute("feed") %>"); 	   
		</script> 
		
    </div>
</div>

<%@include file="footer.jsp" %>